package com.fenghongzhang.welcome;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager vp;
    private LinearLayout ll;
    private TwoFragment twoFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ll = (LinearLayout) findViewById(R.id.ll);
        vp = (ViewPager) findViewById(R.id.vp);

        final List<Fragment> list = new ArrayList<>();
        list.add(new OneFragment());
        list.add(new OneFragment());
         twoFragment = new TwoFragment();
        list.add(twoFragment);


        final List<ImageView> imageViewList = new ArrayList<>();
        //根据list的大小来创建小圆点的数量
        for (int i = 0; i < list.size(); i++) {
            ImageView imageView = new ImageView(this);
            if (i == 0){
                imageView.setImageResource(R.drawable.circle_t);
            }else{
                imageView.setImageResource(R.drawable.circle_f);
            }
            imageViewList.add(imageView);
            ll.addView(imageView);
        }


        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //是最后一页 喊一下倒计时开始
                if(position == list.size()-1){
                 twoFragment.handler.sendEmptyMessage(110);
                }
                //在这里给小圆点,换一个图片
                for (int i = 0; i < imageViewList.size(); i++) {
                    if(i == position){
                        ImageView imageView = imageViewList.get(i);
                        imageView.setImageResource(R.drawable.circle_t);
                    }else{
                        ImageView imageView = imageViewList.get(i);
                        imageView.setImageResource(R.drawable.circle_f);
                    }
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        MyAdapter myAdapter = new MyAdapter(getSupportFragmentManager(), list);
        vp.setAdapter(myAdapter);




    }
}
